import tkinter
import tkinter.messagebox
import os
import os.path

path = os.getenv('temp')
filename = os.path.join(path,'info.txt')

#创建应用窗口
root = tkinter.Tk()
#定义窗口大小
root['height'] = 140
root['width'] = 200

#在窗口上创建标签组件
labelName = tkinter.Label(root,
                          text = '名字',
                          justify = tkinter.RIGHT,
                          anchor = 'e',
                          width = 80)
#把组件置于窗口指定区域
labelName.place(x = 10 , y = 5 , width = 80 , height = 20)

#创建字符串变量和文本框组件，并设置关联变量
#可以通过关联变量读取或修改文本框内容
varName = tkinter.StringVar(root,value = '')
entryName = tkinter.Entry(root,
                          width = 80,
                          textvariable = varName)
entryName.place(x = 100 , y = 5 , width = 80 , height = 20)

labelPwd = tkinter.Label(root,
                         text = '密码',
                         justify = tkinter.RIGHT,
                         anchor = 'e',
                         width = 80)
labelPwd.place(x = 10 , y = 30 , width = 80 , height = 20)

#编写密码文本框
varPwd = tkinter.StringVar(root,value = '')
entryPwd = tkinter.Entry(root,
                         show = '$',#无论输入什么，都显示星号
                         width = 80,
                         textvariable = varPwd)
entryPwd.place(x = 100 , y = 30 , width = 80 , height = 20)

#尝试自动填写用户民和密码
try :
    with open(filename) as fp:
        n , p = fp.read().strip().split(',')
        varName.set(n)
        varPwd.set(p)
except :
    pass

#记住朕，复选框
remeberMe = tkinter.IntVar(root,value = 1)
#选中时变量为1，反之则为0，默认选中。
checkRemeber = tkinter.Checkbutton(root,
                                   text = '记住老子，懂！',
                                   variable = remeberMe,
                                   onvalue = 1,
                                   offvalue = 0)
checkRemeber.place(x = 30 , y = 70 , width = 120 , height = 20)

#登录按钮事件处理函数
def login():
    #获取用户名和密码
    name = entryName.get()
    pwd = entryPwd.get()
    if name == 'xuke' and pwd == '258369' :
        tkinter.messagebox.showinfo(title = '哈哈哈，你终于回来了',
                                    message = '欢迎回家————此地华夏')
        if remeberMe.get() == 1 :
            #将成功登录信息写入临时文件
            with open (filename,'w') as fp :
                fp.write(','.join((name,pwd)))
        else :
            try :
                #删除用于记录用户名和密码的临时文件
                os.remove(filename)
            except :
                pass
    else :
        tkinter.messagebox.showerror('非我族类，其心必异',
                                     message = '华夏禁地，神明禁行')
#创建按钮组件，同时设置按钮事件处理函数
buttonOk = tkinter.Button(root,
                          text = '打入内部',#设置按钮上显示文本
                          command = login)
buttonOk.place(x = 30 , y = 100 , width = 50 ,height = 20)

#取消按钮的事件处理函数
def cancel() :
    #清空用户名和密码
    varName.set('')
    varPwd.set('')
buttonCancel = tkinter.Button(root,
                              text = '速速撤离',
                              command = cancel)
buttonCancel.place(x =90 , y = 100 , width = 50 ,height = 20)

#启动消息循环
root.mainloop()
